import Vue from "vue";
import loadStaticData from '@/util/loadStaticData.js';
export const state = {
	loadingShow: false,
	//微信场景参数
	chatScenesInfo: {},
	//当前位置
	currentAddress: {
		provinceId: "",
		provinceName: "",
		cityId: "",
		cityName: "当阳",
		areaId: "",
		areaName: ""
	},
	$userInfo: {},
	$place: {},
	treeData: [],
	pointList: [],
	accessToken:''
};

export const getters = {};

//缓存浏览器的数据名称
const cacheNameList = ["$userInfo"];
let clearTime;
export const mutations = {
	setPointList(state, data) {
		state.pointList = data
	},
	//取出缓存数据（打开APP就取出）
	setCacheData(state) {
		for (let name of cacheNameList) {
			let data;
			// #ifndef H5
			data = uni.getStorageSync(name);
			// #endif
			// #ifdef H5
			data = sessionStorage.getItem(name) || localStorage.getItem(name);
			// #endif
			if (data) {
				// #ifdef H5
				try {
					data = JSON.parse(data);
				} catch (e) {}
				// #endif
				state[name] = data;
			}
		}
	},
	//数据加载状态
	setLoadingShow(state, data) {
		if (state.loadingShow) {
			clearTime && clearTimeout(clearTime);
			clearTime = setTimeout(function() {
				state.loadingShow = data;
			}, 300);
		} else {
			state.loadingShow = data;
		}
	},
	//微信场景参数
	setChatScenesInfo(state, data) {
		if (data) {
			state.chatScenesInfo = data;
		}
	},
	//当前地址
	setCurrentAddress(state, data) {
		if (data) {
			state.currentAddress = Object.assign(state.currentAddress, data);
			let addressInfo = {
				"provinceId": state.currentAddress.provinceId,
				"provinceName": state.currentAddress.provinceName,
				"cityId": state.currentAddress.cityId,
				"cityName": state.currentAddress.cityName,
				"areaId": state.currentAddress.areaId,
				"areaName": state.currentAddress.areaName,
			};
			uni.setStorageSync('currentAddress', addressInfo);
		}
	},
	setTreeData(state, data) {
		if (data) {
			console.log(data, 'data,,,');
			state.treeData = [data];
		}
	},

};
export const actions = {
	getTreeData(context) {
		return new Promise((resolve, reject) => {
			loadStaticData('tree_union.json')
				.then((result) => {
					context.commit("setTreeData", result);
					resolve();
				})
				.catch(() => {
					reject();
				});
		});
	}
};